import {
    isNotEmpty
} from "../../../common/utils/util";

export default class UnitDetailModel {
    constructor(param) {
        const {
            id, // id
            userId,
            userName,
            title,
            summary,
            content, // 中文内容
            contentMd, // 中文内容 无标签版
            englishTitle, // 英文标题
            englishContent, // 英文内容
            englishContentMd, // 英文内容 无标签版
            avatar, // 文章图片
            originalAudio, // 原文朗读 音频
            originalExplanationAudio, // 原文讲解 音频
            originalExplanationPdf, // 原文讲解PDF
        } = param || {}
        this.id = id;
        this.userId = userId;
        this.userName = userName;
        this.summary = summary;
        this.title = title;
        this.content = content;
        this.contentMd = contentMd;
        this.englishTitle = englishTitle;
        this.englishContent = englishContent;
        this.englishContentMd = englishContentMd;
        this.avatar = avatar;
        this.originalAudio = originalAudio;
        this.originalExplanationAudio = originalExplanationAudio;
        this.originalExplanationPdf = originalExplanationPdf;
    }

    dataProcessing(wordList) {
        this.pointWordList = wordList;

        // 文章内容
        this.cnArticle = processWordFn(this.title, this.content, []);
        this.enArticle = processWordFn(this.englishTitle, this.englishContent, wordList);

        // 朗读题
        let sentenceResult = processSentenceFn(this.englishTitle, this.englishContent)
        this.aloudArticle = sentenceResult.result;
        this.aloudArticleTotal = sentenceResult.total;
    }
}

// 跟读长句处理函数
function processSentenceFn(title, content) {
    let result = [];
    let index = 0;
    // 插入标题
    result.push({
        tag: "h1",
        content: title,
        index
    })
    // 替换掉所有换行符
    content = content.replace(/\n/g, "");
    content = content.replace(/\s+/g, " ");
    // 读取所有p标签内容
    let pList = content.match(/[^><]+(?=<\/p>)/img);
    // 遍历段落内容
    pList.forEach((pItem, pIndex) => {
        let sentenceList = pItem.split(".").filter(tempItem => {
            return tempItem != ""
        }); // 划分句子
        if (sentenceList[sentenceList.length - 1] == "") {
            sentenceList.pop(); // 最后有个空字符，需要去除
        }
        let tempResult = [];
        let childs = [];
        // 遍历句子列表
        sentenceList.forEach((sItem, sIndex) => {
            // 插入临时列表等待处理
            tempResult.push(sItem);
            if (sIndex % 2 != 0) { // 双数处理
                // 插入child
                childs.push({
                    tag: "text",
                    content: `${tempResult.join(". ")}. `,
                    index: ++index
                })
                // 清空暂存表
                tempResult = [];
            }
        })
        // 处理完一个段落
        result.push({
            tag: "p",
            childs
        })
    })
    // 返回结果
    return {
        result,
        total: index + 1
    };
}

// 重点词汇处理函数
function processWordFn(title, content, wordList) {
    let result = [];
    // 插入标题
    result.push({
        tag: "h1",
        content: title,
    })
    // 替换掉所有换行符
    content = content.replace(/\n/g, "");
    content = content.replace(/\s+/g, " ");
    // 读取所有p标签内容
    let tempList = content.match(/[^><]+(?=<\/p>)/img);
    // 遍历段落内容
    tempList.forEach((item, index) => {
        // 判断重点词汇列表是否为空
        if (isNotEmpty(wordList)) {
            // 如果有重点词汇，段落需要拆分成child列表
            let pWords = item.split(/\s+/).filter(tempItem => {
                return tempItem != ""
            }); // 根据空格将段落内容拆分成单个单词
            let processChildFn = (unprocessWords) => {
                let childs = []; // 结果
                let temp = []; // 暂存已处理非重点词汇
                // 双重遍历未处理词汇及重点单词，并进行比对
                unprocessWords.forEach(word => { // 未处理词汇
                    let matchPointWord = null;
                    for (let pointIndex = 0; pointIndex < wordList
                        .length; pointIndex++) { // 重点单词
                        let pointWord = wordList[pointIndex];
                        // 判断是否是重点词汇
                        if (word.toLowerCase().replace(/[^\w]|_/g, '') == pointWord
                            .englishWord) {
                            matchPointWord = pointWord;
                            break;
                        }
                    }
                    if (matchPointWord) {
                        // 如果是重点词汇
                        // 1. 将重点词汇之前的结果拼接并存入child
                        childs.push({
                            tag: "text",
                            content: temp.join(" "),
                        })
                        // 2. 将重点词汇存入child
                        childs.push({
                            tag: 'text point',
                            content: ` ${word} `,
                            data: matchPointWord
                        })
                        // 3. 清空temp
                        temp = [];
                    } else {
                        // 如果不是重点词汇，存入临时列表
                        temp.push(word);
                    }
                })
                // 最后可能有一部分非重点词汇，进行最后处理
                if (isNotEmpty(temp)) {
                    childs.push({
                        tag: "text",
                        content: temp.join(" "),
                    })
                }
                return childs;
            }
            result.push({
                tag: "p",
                childs: processChildFn(pWords)
            })
        } else {
            // 如果没有重点词汇，不需要处理直接插入段落内容，
            result.push({
                tag: "p",
                content: item
            })
        }
    })
    return result;
}